﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;

namespace Light.Data.DataEntity
{
    /// <summary>
    /// 用于数据实体更新，插入，删除的命令类
    /// </summary>
   public  class DataEntityCommand
    {
       private IDatabase _database = null;

       public DataEntityCommand(IDatabase database)
       {
           _database = database;
       }
       private string _insertCommand = string.Empty;
       public string InsertCommand
       {
           get
           {
               return _insertCommand;
           }
           set
           {
               _insertCommand = value;
           }
       }

       private string _updateCommand = string.Empty;
       public string UpdateCommand
       {
           get
           {
               return _updateCommand;
           }
           set
           {
               _updateCommand = value;
           }
       }

       private string _deleteCommand = string.Empty;
       public string DeleteCommand
       {
           get
           {
               return _deleteCommand;
           }
           set
           {
               _deleteCommand = value;
           }
       }
       private string[] _primaryKeys=null;
       public string[] PrimaryKeys
       {
           get
           {
               return _primaryKeys;
           }
           set
           {
               _primaryKeys = value;
           }
       }
       private Dictionary<string, DbParameter> _nameDbParameter = new Dictionary<string, DbParameter>();

       public Dictionary<string, DbParameter> Parameters
       {
           get
           {
               return _nameDbParameter;
           }
           set
           {
               _nameDbParameter = value;
           }
       }
       /// <summary>
       /// 输出所有DbParameter
       /// </summary>
       /// <returns></returns>
       public DbParameter[] ToDbParameters()
       {
           if (_nameDbParameter == null)
               return null;
           DbParameter[] parameter=new DbParameter[_nameDbParameter.Count];

           _nameDbParameter.Values.CopyTo(parameter, 0);
           
           return parameter;
       }
       /// <summary>
       /// 仅输出主键DbParameter，用于删除,更新操作时Where语句的参数
       /// </summary>
       /// <returns></returns>
       public DbParameter[] ToPrimaryKeyDbParameters()
       {
           if (_primaryKeys == null)
               return null;
           DbParameter[] parameter = new DbParameter[_primaryKeys.Length];

           for (int i = 0; i < _primaryKeys.Length; i++)
           {
               parameter[i] = _nameDbParameter[_primaryKeys[i]];
           }
           return parameter;
       }
       /// <summary>
       /// 重置DbParameter
       /// </summary>
       public void ResetParameters()
       {
           var parameters= new Dictionary<string, DbParameter>();
           foreach (var item in _nameDbParameter)
           {
               parameters[item.Key] = _database.BuildDbParameter(item.Key, item.Value.DbType);
           }

           this.Parameters = parameters;
       }
    }
}
